<%@page contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>预算编制</title>
<%@ include file="../../common/common.jsp"%>


<script type="text/javascript" 
src="<%=basePath%>/js/common/common.js"></script>

<script type="text/javascript" 
src="<%=basePath%>/js/qn/js_open_budget_do.js"></script>
	
<script type="text/javascript">


//工程信息Id
var project_workinfo_id = parent.document.getElementById('projectWorkInfoId').value;
  
//项目id
var project_id = parent.document.getElementById('projectId').value;


<%-- //施工人员消耗 计算
function _fun_cal_sgry_xh(item_name,v){
	var item_name = 'sgry_xh';
	var params = {budget_item_name:item_name,arg:v} ;
	var url = "<%=basePath%>calItemAction!exe_sgry_xh.do";
	jQuery.post(url,params,function(data){
		//alert(data.result);
		$('#dg').datagrid('updateRow',{index:1,row:{budget_do_quantities:data.budget_do_quantities}});
		$('#dg').datagrid('updateRow',{index:1,row:{totalAmount:data.totalAmount}});
	},'json');	
}



//渣土运输 计算
function _fun_cal_ztys(item_name,v){
	var item_name = 'hfc_ztys';
	var params = {budget_item_name:item_name,arg:v} ;
	var url = "<%=basePath%>calItemAction!exe_ztys.do";
	jQuery.post(url,params,function(data){
		//alert(data.budget_do_quantities);
		$('#dg').datagrid('updateRow',{index:2,row:{budget_do_quantities:data.budget_do_quantities}});
		$('#dg').datagrid('updateRow',{index:2,row:{totalAmount:data.totalAmount}});
	},'json');	
} --%>

//平均有效容积计算，并保存
function _fun_calAvg(){
	
	var num = $('#budget_num').val();
	
	if(num==''){
		$.messager.alert("提示","请输入化粪池数量");
		return;
	}
	
	var budget_effectiveCapacity = $('#budget_effectiveCapacity').val();
	//算平均
	var budget_avgCapacity =  Math.round(budget_effectiveCapacity/num);
	//四舍五入
	budget_avgCapacity = budget_avgCapacity.toFixed(2);
	
	
	//赋值
	$('#budget_avgCapacity').attr('value',budget_avgCapacity);
	
	var params = {
			project_workinfo_id:project_workinfo_id,
			avgCapacity:$('#budget_avgCapacity').val(),
			num:$('#budget_num').val(),
			effectiveCapacity:$('#budget_effectiveCapacity').val()
			};
	var url = "<%=basePath %>workQuantitiesAction!calAndsaveAvg.do";
	
	jQuery.post(url,params,function(data){
			//alert(data.message);
			 if(data.message=='true'){
			
			}
		
	},'json');
	
}




//保存工程量计算结果
function save_quantities(){
	var rows = $('#dg—gcl').datagrid('getRows');
	var quantitiesItemList = $.toJSON(rows);
	//使用异步程序，在后台进行计算	
	var params = {
			project_workinfo_id:project_workinfo_id,
			wasteWaterCapacity:$('#wasteWaterCapacity').val(),
			sludgeCapacity:$('#sludgeCapacity').val(),
			effectiveCapacity:$('#effectiveCapacity').val(),
			quantitiesBuildItemList:quantitiesItemList
			};
	var url = "<%=basePath %>workQuantitiesAction!saveQuantities.do";
	
	jQuery.post(url,params,function(data){
			//alert(data.message);
			 if(data.message=='true'){
				  /* $.messager.confirm('提示','保存成功，您是否继续进行预算表编制',function(r){  
					      if (r){  
					    	  //选择tab
					         $('#tt').tabs('select','预算表');
					    	 //渣土运输
					    	 // _fun_cal_ztys('',$('#sludgeCapacity').val());
					    	 //施工人员消耗 计算
					    	 // _fun_cal_sgry_xh('',$('#effectiveCapacity').val());
					    	 
					      }  
					 });   */
				 $.messager.alert('提示','保存成功');	 

			}
		
	},'json');
			
}


// 预算表保存
function save_budget_do_info(){
	//使用异步程序，在后台进行计算
	var rows = $('#dg').datagrid('getRows');
	var arrayParam ='';
	for(i=0;i<rows.length;i++){
		var json2String = O2String(rows[i]);
		arrayParam = arrayParam+json2String+"|";
	}
	//alert(arrayParam);
	var url = "<%=basePath%>saveBudgetDoInfoAction.do";
	var params = {rowlist:arrayParam,project_workinfo_id:project_workinfo_id} ;
	jQuery.post(url,params,function(data){
		//alert(data.message);
		 if(data.message=='true'){
			/*   $.messager.confirm('提示','保存成功，您是否继续进行取费表编制',function(r){  
				      if (r){  
				    	  //选择tab
				         $('#tt').tabs('select','取费表');
				      }  
				 });   */
				 
			$.messager.alert('提示','保存成功');

		}else{
			$.messager.alert('提示','保存失败，请联系管理员');
		}
		
		
	},'json');	
			
}

// 取费表保存
function save_budget_fee_do(){
	//使用异步程序，在后台进行计算
	var rows = $('#dg—qfb').datagrid('getRows');
	var arrayParam ='';
	for(i=0;i<rows.length;i++){
		var json2String = O2String(rows[i]);
		arrayParam = arrayParam+json2String+"|";
	}
	//alert(arrayParam);
	var url = "<%=basePath%>saveFeeItemDoAction.do";
	var params = {rowlist:arrayParam,project_workinfo_id:project_workinfo_id,project_id:project_id} ;
	jQuery.post(url,params,function(data){
		//alert(data.message);
		if(data.message=='true'){
			 $.messager.confirm('提示','编制完成，是否结束',function(r){  
			      if (r){  
			    	  window.close(); // close the dialog
	    			  parent.window.location.reload(); // reload the user data
			      }  
			 });
			 
		}
		//$('#dg').datagrid('updateRow',{index:2,row:{budget_do_quantities:data.budget_do_quantities}});
		//$('#dg').datagrid('updateRow',{index:2,row:{totalAmount:data.totalAmount}});
	},'json');	
			
}









//工程量-建筑类型保存-计算
//var isCal_quanties_do = false;

function _cal_quantities_do2save(){
	var rows = $('#dg—gcl').datagrid('getRows');
	for(var i=0;i<rows.length;i++){
		$('#dg—gcl').datagrid('endEdit',i);
	}
	var selects = $('#dg—gcl').datagrid('getRows');
   	  var url = "<%=basePath %>workQuantitiesAction!calQuantities.do";
   		var quantitiesItemList = $.toJSON(selects);
   		//alert(quantitiesItemList);
   		var params ={quantitiesBuildItemList:quantitiesItemList};
   		jQuery.post(url,params,function(result){
   			if(result!=''){
   				$('#wasteWaterCapacity').attr('value',result.wasteWaterCapacity);
   				$('#sludgeCapacity').attr('value',result.sludgeCapacity);
   				$('#effectiveCapacity').attr('value',result.effectiveCapacity);
   				
   				 $.messager.confirm('提示','计算完成，请确认是否保存信息',function(r){  
   				      if (r){  
   				    	  save_quantities();
   				      }  
	   				 }); 
	   			
   				
   			}
   			
   		},'json');	
   	  

	
}




//预算表计算
//var isCal_budget_do = false;
function _cal_budget_do2save(){
	//isCal_budget_do = true;
	var rows =  $('#dg').datagrid('getRows');
	var _totalAmount =0.0;
	for(i=0;i<rows.length-1;i++){
		//判断工程量是否为空
		var tmp = rows[i].totalAmount;
	
		if(!isNaN(tmp)){
			_totalAmount = _totalAmount+parseFloat(tmp);
		}
	}
	 $.messager.confirm('提示','计算完成，请确认是否保存信息',function(r){  
	      if (r){  
	    	  save_budget_do_info();	
	      }  
	 }); 
	//alert('totalAmount:'+_totalAmount);
	$('#dg').datagrid('updateRow',{index:rows.length-1,row:{totalAmount:_totalAmount}});			
}

//取费表计算

function _cal_fee_do2save(){	
	var url = "<%=basePath%>calFeeItemAction.do";
	var params = {project_workinfo_id:project_workinfo_id} ;
	jQuery.post(url,params,function(data){
		//alert(data.feeItemList);
		if(data!=null){
			var itemList = data.feeItemList ;
			for(i=0;i<itemList.length;i++){
				var jsonobj = itemList[i];	
				//alert(jsonobj.fee_amount);
				$('#dg—qfb').datagrid('updateRow',{index:i,row:{fee_amount:jsonobj.fee_amount}});
			}
			$.messager.confirm('提示','计算完成，请确认是否保存信息',function(r){  
			      if (r){  
			    	  save_budget_fee_do();	
			      }  
			 }); 
		}
	},'json');	
			
}


function _fun_confirm (){
	$.messager.confirm('提示','请确认是否已经保存了添加信息',function(r){  
	      if (r){  
	    	  window.close(); // close the dialog
			  parent.window.location.reload(); // reload the user data
	      }  
	 }); 	
}

function _fun_close(){
	 window.close(); // close the dialog
	 parent.window.location.reload(); // reload the user data
}

function init_quantities(){
	$(function(){
		//异步获取信息
		//使用异步程序，在后台进行计算	
		var params = {
				project_workinfo_id:project_workinfo_id
				};
		var url = baseURL+"/workQuantitiesAction!loadQuantities.do";
		
		jQuery.post(url,params,function(data){
				if(data!=''){
						
					//预算表页面中
					$('#budget_effectiveCapacity').attr('value',data.effectiveCapacity);
					$('#budget_num').attr('value',data.num);
					$('#budget_avgCapacity').attr('value',data.avgCapacity);	
					
				}
			
		},'json');
			
	});
}

$(function(){
	 $('#tt').tabs({ 
		  border:false, 
		  onSelect:function(title){ 
			//alert(title);
		  	if(title=='预算表'){
		  		init_quantities();
		  	}
		  } 

	  });
})


</script>
</head>
	
<body>			
	<div id="tt" class="easyui-tabs" style="width:720px;height:380px; padding-top: 0px;" tools="#tab-tools" >

	<div title="工程量" closable="false" style="overflow:hidden">
		
	<form id="f_gcl" method="post">
		<input type="hidden" name="project_workinfo_id" id="project_workinfo_id">
				
	 	
	 	<table style="border:1px solid #b3dfda;width: 710px;" id="t_result" title="结果">
					<tr>
						<td>
							<div class="fitem">
								<label>
									污水容积:
								</label>
								<input id="wasteWaterCapacity" name="wasteWaterCapacity" type="text" readonly="readonly" size="12">m<sup>3</sup> 
							</div>
						</td>
						<td>
							<div class="fitem">
								<label>
									污泥容积:
								</label>
								<input id="sludgeCapacity" name="sludgeCapacity" type="text" readonly="readonly" size="12">m<sup>3</sup> 
							</div>
						</td>
						
						<td>
							<div class="fitem">
								<label>
									有效容积:
								</label>
							
								<input id="effectiveCapacity" name="effectiveCapacity" type="text" readonly="readonly" size="12">m<sup>3</sup> 
							</div>
						</td>

					</tr>
				
					
		</table>
	 	
	 	<table id="dg—gcl" style="width:720px;height:280px" 
				title="建筑类型"  fitColumns="true"   >
			<thead>
				<tr>
					<th field="item_id" width="5" align="center" >序号</th>
					<th field="item_name" align="center" width="20" hidden="true"></th>
					<th field="item_value" align="center" width="20">名称</th>
					<th field="v_data" width="15" editor="{type:'validatebox'}">值</th>
					<th field="v_unit" width="15" >单位</th>
					
				</tr>
			</thead>
		</table>
	 	<div align="center">	
	 		<a href="#" class="easyui-linkbutton" iconCls="icon-ok"
				onclick="_cal_quantities_do2save()">保存</a>
	   </div>	
	 		
		</form>
	</div>
	
	<div title="预算表" closable="false" style="overflow:hidden">
	
	
		 	<table style="border:1px solid #b3dfda;width: 710px;" id="t_result" title="结果">
					<tr>
						
						
						
						<td>
							<div class="fitem">
								<label>
									有效容积:
								</label>
							
								<input id="budget_effectiveCapacity" name="budget_effectiveCapacity" type="text" readonly="readonly" size="12">m<sup>3</sup> 
							</div>
						</td>
						
						<td>
							<div class="fitem">
								<label>
									化粪池个数:
								</label>
								<input id="budget_num" name="budget_num" type="text"  size="12">个 
							</div>
						</td>
						
						<td>
							<div class="fitem">
								<label>
									平均容积:
								</label>
								<input id="budget_avgCapacity" name="budget_avgCapacity" type="text" readonly="readonly" size="12">m<sup>3</sup> 
							</div>
						</td>
						
						<td>
							<a href="#" class="easyui-linkbutton" iconCls="icon-ok" onclick="_fun_calAvg()">计算</a>
						</td>

					</tr>
				
					
		</table>
	
		<table id="dg" style="width:720px;height:280px" 
				title="工程预算表" fitColumns="true"  singleSelect="true" >
			<thead>
				<tr>
					<th field="item_id" width="5" align="center" >序号</th>
					<th field="budget_item_name" align="center" width="40" hidden="true"></th>			
					<th field="budget_item_value" align="center" width="40">名称</th>
					<th field="item_unit" align="center" width="10">单位</th>
					<th field="budget_do_quantities" width="15" >工程量</th>
					<th field="item_unit_price" width="20" align="center">单价(元)</th>
					<th field="totalAmount" width="20" align="center">合价(元)</th>
			
				</tr>
			</thead>
			
		</table>
		
		<div align="center">	
	 		<a href="#" class="easyui-linkbutton" iconCls="icon-ok"
				onclick="_cal_budget_do2save()">保存</a>
	   </div>	
		
	</div>
		
	<div title="取费表" closable="false" style="overflow:hidden">
		<table id="dg—qfb" style="width:720px;height:320px" 
				title="取费表"
				singleSelect="true" fitColumns="true"   >
			<thead>
				<tr>
					<th field="item_id" width="5" align="center" >序号</th>
					<th field="fee_item_name" align="center" width="20" hidden="true"></th>
					<th field="fee_item_value" align="center" width="20">取费名称</th>
					<th field="fee_formula" align="center" width="20">取费公式</th>
					<th field="item_rate" width="15" >费率</th>
					<th field="fee_amount" width="20" align="center">金额</th>
					
				</tr>
			</thead>
		</table>
		
		<div align="center">	
	 		<a href="#" class="easyui-linkbutton" iconCls="icon-ok"
				onclick="_cal_fee_do2save()">保存</a>
	   </div>	
				
	</div>
		
		
		
	</div>
    


			
		
	</body>
</html>